﻿/*Trang này chỉ chỉ tài khoản của Phát triển web và quản trị web mới được vào
 * Nhưng chỉ có tài khoản của Phát triển web mới có quyền xóa còn Quản trị web không có quyền xóa
 */
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

using Business = VietStar.Library.Business;
using Entity = VietStar.Library.Entity;
using Common = VietStar.Library.Commom;
public partial class controls_admin_listmenu : System.Web.UI.UserControl
{
    Common.Algorithm.Validators validator = new Common.Algorithm.Validators();
    DataTable dsDanhMucCha = new DataTable();
    DataTable dsDanhMucCon1 = new DataTable();
    DataTable dsDanhMucCon2 = new DataTable();
    DataTable dsTinTuc = new DataTable();
    DataTable dsBinhLuan = new DataTable();

    Business.ChuyenMuc business = new Business.ChuyenMuc();
    Business.TinTuc businessTinTuc = new Business.TinTuc();
    Business.BinhLuan businessBinhLuan = new Business.BinhLuan();

    Entity.ChuyenMuc entityDanhMucCha = new Entity.ChuyenMuc();
    Entity.ChuyenMuc entityDanhMucCon1 = new Entity.ChuyenMuc();
    Entity.ChuyenMuc entityDanhMucCon2 = new Entity.ChuyenMuc();
    Entity.ChuyenMuc entity = new Entity.ChuyenMuc();
    Entity.TinTuc entityTintuc = new Entity.TinTuc();
    Entity.BinhLuan entityBinhLuan = new Entity.BinhLuan();
    
    public int stt = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        // Chỉ có Phát triển web và quản trị web mới được vào trang này
        if (Convert.ToInt32(Session["role_login_admin"].ToString()) == 1 || Convert.ToInt32(Session["role_login_admin"].ToString()) == 2)
        {
            if (!IsPostBack)
            {
                LoadDanhMuc();
            }
            if (Convert.ToInt32(Session["role_login_admin"].ToString()) == 2)
            {
                btnXoa.Visible = false;
            }
        }
        else
        {
            Response.Redirect("listnews.aspx");
        }
    }
    protected void LoadDanhMuc()
    {
        if (Convert.ToInt32(Session["role_login_admin"].ToString()) == 2)
        {
            entityDanhMucCha.DanhMucCha = 0;
            dsDanhMucCha = business.SelectByDanhMucCha(entityDanhMucCha);
            rptDanhMucCha.DataSource = dsDanhMucCha;
            rptDanhMucCha.DataBind();
            for (int i = 0; i < dsDanhMucCha.Rows.Count; i++)
            {
                entityDanhMucCon1.DanhMucCha = Convert.ToInt32(dsDanhMucCha.Rows[i]["Id"].ToString());
                dsDanhMucCon1 = business.SelectByDanhMucCha(entityDanhMucCon1);
                Repeater rptCon1 = (Repeater)rptDanhMucCha.Items[i].FindControl("rptDanhMucCon1");
                rptCon1.DataSource = dsDanhMucCon1;
                rptCon1.DataBind();
                for (int j = 0; j < dsDanhMucCon1.Rows.Count; j++)
                {
                    entityDanhMucCon2.DanhMucCha = Convert.ToInt32(dsDanhMucCon1.Rows[j]["Id"].ToString());
                    dsDanhMucCon2 = business.SelectByDanhMucCha(entityDanhMucCon2);
                    Repeater rptCon2 = (Repeater)rptCon1.Items[j].FindControl("rptDanhMucCon2");
                    rptCon2.DataSource = dsDanhMucCon2;
                    rptCon2.DataBind();
                    if (dsDanhMucCon2.Rows.Count > 0)
                    {
                        for (int k = 0; k < dsDanhMucCon2.Rows.Count; k++)
                        {
                            HyperLink hplCon2 = (HyperLink)rptCon2.Items[k].FindControl("lbXoa");
                            hplCon2.Visible = false;
                        }
                    }
                    HyperLink hplCon1 = (HyperLink)rptCon1.Items[j].FindControl("lbXoa");
                    hplCon1.Visible = false;
                }
                HyperLink hplCha = (HyperLink)rptDanhMucCha.Items[i].FindControl("lbXoa");
                hplCha.Visible = false;
            }
        }
        else
        {
            entityDanhMucCha.DanhMucCha = 0;
            dsDanhMucCha = business.SelectByDanhMucCha(entityDanhMucCha);
            rptDanhMucCha.DataSource = dsDanhMucCha;
            rptDanhMucCha.DataBind();
            for (int i = 0; i < dsDanhMucCha.Rows.Count; i++)
            {
                entityDanhMucCon1.DanhMucCha = Convert.ToInt32(dsDanhMucCha.Rows[i]["Id"].ToString());
                dsDanhMucCon1 = business.SelectByDanhMucCha(entityDanhMucCon1);
                Repeater rptCon1 = (Repeater)rptDanhMucCha.Items[i].FindControl("rptDanhMucCon1");
                rptCon1.DataSource = dsDanhMucCon1;
                rptCon1.DataBind();
                for (int j = 0; j < dsDanhMucCon1.Rows.Count; j++)
                {
                    entityDanhMucCon2.DanhMucCha = Convert.ToInt32(dsDanhMucCon1.Rows[j]["Id"].ToString());
                    dsDanhMucCon2 = business.SelectByDanhMucCha(entityDanhMucCon2);
                    Repeater rptCon2 = (Repeater)rptCon1.Items[j].FindControl("rptDanhMucCon2");
                    rptCon2.DataSource = dsDanhMucCon2;
                    rptCon2.DataBind();                    
                }               
            }
        }
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/admin/addmenu.aspx?action=add&info=0");
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        //Duyệt tất cả các item trong repeater
        foreach (RepeaterItem listItem in rptDanhMucCha.Items)
        {               
            CheckBox cb = listItem.FindControl("CheckSelect") as CheckBox;
            //Nếu trạng thái của checkbox  là Checked
            if (cb.Checked)
            {
                int id = Convert.ToInt32((listItem.FindControl("ltId") as Literal).Text);
                entityDanhMucCha.Id = id;
                entityDanhMucCon1.DanhMucCha = id;
                dsDanhMucCon1 = business.SelectByDanhMucCha(entityDanhMucCon1);
                if (dsDanhMucCon1.Rows.Count > 0)
                {
                    for (int j = 0; j < dsDanhMucCon1.Rows.Count; j++)
                    {
                        entityDanhMucCon2.DanhMucCha = Convert.ToInt32(dsDanhMucCon1.Rows[j]["Id"].ToString());
                        dsDanhMucCon2 = business.SelectByDanhMucCha(entityDanhMucCon2);
                        if (dsDanhMucCon2.Rows.Count > 0)
                        {
                            for (int k = 0; k < dsDanhMucCon2.Rows.Count; k++)
                            {
                                //entityDanhMucCon2.Id = Convert.ToInt32(dsDanhMucCon2.Rows[k]["Id"].ToString());
                                //business.Delete(entityDanhMucCon2);
                                int id3 = Convert.ToInt32(dsDanhMucCon2.Rows[k]["Id"].ToString());
                                DeleteFK(id3);
                            }
                            //entityDanhMucCon1.Id = Convert.ToInt32(dsDanhMucCon1.Rows[j]["Id"].ToString());
                            //business.Delete(entityDanhMucCon1);
                            int id2 = Convert.ToInt32(dsDanhMucCon1.Rows[j]["Id"].ToString());
                            DeleteFK(id2);
                        }
                        else
                        {
                            //entityDanhMucCon1.Id = Convert.ToInt32(dsDanhMucCon1.Rows[j]["Id"].ToString());
                            //business.Delete(entityDanhMucCon1);
                            int id1 = Convert.ToInt32(dsDanhMucCon1.Rows[j]["Id"].ToString());
                            DeleteFK(id1);
                        }
                    }
                    //business.Delete(entityDanhMucCha);
                    DeleteFK(id);
                }
                else
                {
                   //business.Delete(entityDanhMucCha);  
                    DeleteFK(id);
                }
            }            
        }
        Response.Redirect(Request.Url.ToString());
    }
    protected void DeleteFK(int id)
    {
        try
        {
            entityTintuc.ChuyenMuc = id;
            dsTinTuc = businessTinTuc.SelectByDanhMuc(entityTintuc);
            if (dsTinTuc.Rows.Count > 0)
            {
                for (int i = 0; i < dsTinTuc.Rows.Count; i++)
                {
                    entityBinhLuan.BaiViet = Convert.ToInt32(dsTinTuc.Rows[i]["Id"].ToString());
                    dsBinhLuan = businessBinhLuan.SelectTheoBaiViet(entityBinhLuan);
                    if (dsBinhLuan.Rows.Count > 0)
                    {
                        for (int j = 0; j < dsBinhLuan.Rows.Count; j++)
                        {
                            entityBinhLuan.Id = Convert.ToInt32(dsBinhLuan.Rows[j]["Id"].ToString());
                            businessBinhLuan.Delete(entityBinhLuan);
                        }
                        entityTintuc.Id = Convert.ToInt32(dsTinTuc.Rows[i]["Id"].ToString());
                        businessTinTuc.Delete(entityTintuc);
                    }
                    else
                    {
                        entityTintuc.Id = Convert.ToInt32(dsTinTuc.Rows[i]["Id"].ToString());
                        businessTinTuc.Delete(entityTintuc);
                    }
                }
                entity.Id = id;
                business.Delete(entity);
            }
            else
            {
                entity.Id = id;
                business.Delete(entity);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnSapXep_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (RepeaterItem listItem in rptDanhMucCha.Items)
            {
                TextBox tb = (TextBox)listItem.FindControl("txtSapXep");
                Repeater rpt1 = listItem.FindControl("rptDanhMucCon1") as Repeater;
                if (rpt1.Items.Count > 0)
                {
                    foreach (RepeaterItem listItem1 in rpt1.Items)
                    {
                        TextBox tb1 = (TextBox)listItem1.FindControl("txtSapXep");
                        Repeater rpt2 = listItem1.FindControl("rptDanhMucCon2") as Repeater;
                        if (rpt2.Items.Count > 0)
                        {
                            foreach (RepeaterItem listItem2 in rpt2.Items)
                            {
                                TextBox tb2 = (TextBox)listItem2.FindControl("txtSapXep");
                                entityDanhMucCon2.Id = Convert.ToInt32((listItem2.FindControl("ltId") as Literal).Text);
                                entityDanhMucCon2.SapXep = Convert.ToInt32(tb2.Text);
                                business.UpdateSapXep(entityDanhMucCon2);
                            }                            
                            entityDanhMucCon1.Id = Convert.ToInt32((listItem1.FindControl("ltId") as Literal).Text);
                            entityDanhMucCon1.SapXep = Convert.ToInt32(tb1.Text);
                            business.UpdateSapXep(entityDanhMucCon1);
                        }
                        else
                        {                            
                            entityDanhMucCon1.Id = Convert.ToInt32((listItem1.FindControl("ltId") as Literal).Text);
                            entityDanhMucCon1.SapXep = Convert.ToInt32(tb1.Text);
                            business.UpdateSapXep(entityDanhMucCon1);
                        }
                    }
                    entityDanhMucCha.Id = Convert.ToInt32((listItem.FindControl("ltId") as Literal).Text);
                    entityDanhMucCha.SapXep = Convert.ToInt32(tb.Text);
                    business.UpdateSapXep(entityDanhMucCha);
                }
                else
                {
                    entityDanhMucCha.Id = Convert.ToInt32((listItem.FindControl("ltId") as Literal).Text);
                    entityDanhMucCha.SapXep = Convert.ToInt32(tb.Text);
                    business.UpdateSapXep(entityDanhMucCha);
                }
            }
            LoadDanhMuc();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
